package com.liuqi.openai.data.embedding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.liuqi.openai.util.ValidationUtil.ensureNotNull;

/**
 * 表示文本的密集矢量嵌入
 *
 * @author liuqi
 * @date 2025/7/20
 **/
public class Embedding {

    private final float[] vector;

    public Embedding(float[] vector) {
        this.vector = ensureNotNull(vector, "vector");
    }

    public float[] vector() {
        return vector;
    }

    public List<Float> vectorAsList() {
        List<Float> list = new ArrayList<>(vector.length);
        for (float f : vector) {
            list.add(f);
        }
        return list;
    }

    public int dimensions() {
        return vector.length;
    }

    public static Embedding from(float[] vector) {
        return new Embedding(vector);
    }

    public static Embedding from(List<Float> vector) {
        float[] array = new float[vector.size()];
        for (int i = 0; i < vector.size(); i++) {
            array[i] = vector.get(i);
        }
        return new Embedding(array);
    }

    @Override
    public String toString() {
        return "Embedding{" +
                "vector=" + Arrays.toString(vector) +
                '}';
    }
}
